using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OpenFlashChartLib;
using System.Collections.Generic;

namespace WebDemo
{
    public partial class DemoJQuery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.RegisterClientScriptInclude("jquery", Page.ResolveUrl("~/js/jquery-1.2.1.pack.js"));
            ClientScript.RegisterClientScriptInclude("flash", Page.ResolveUrl("~/js/jquery.flash.js"));
            if (!IsPostBack)
            {
                ddlGraphType.Items.Add(new ListItem("Bar chart", BarGraphType.BarGraphToken));
                ddlGraphType.Items.Add(new ListItem("Glass chart", GlassGraphType.GlassGraphToken));
                ddlGraphType.Items.Add(new ListItem("3D chart", ThreeDimGraphType.ThreeDimGraphToken));
                ddlGraphType.Items.Add(new ListItem("Pie chart", PieGraphType.PieGraphToken));
                ddlGraphType.Items.Add(new ListItem("Fade Bar", FadeBarGraphType.FadeBarGraphToken));
                ddlGraphType.Items.Add(new ListItem("Sketch Bar", SketchGraphType.SketchGraphToken));
                ddlGraphType.Items.Add(new ListItem("Lines", LineGraphType.LineGraphToken));
                ddlGraphType.Items.Add(new ListItem("Dot Lines", DotLineGraphType.DotLineGraphToken));
                ddlGraphType.Items.Add(new ListItem("Hollow Lines", HollowLineGraphType.HollowLineGraphToken));
            }
            ofcTest.BuildChart();
            ofcTest.ScriptRegister += new RegisterScriptEventHandler(ofcTest_ScriptRegister);
        }

        void ofcTest_ScriptRegister(object sender, RegisterScriptEventArgs e)
        {
            ScriptManager.RegisterStartupScript(Page, GetType(), "embed",  e.ScriptBody, true);
        }

        protected void ofcTest_DrawChart(object sender, OpenFlashChartLib.DrawChartEventArgs e)
        {
            Graph graph = new Graph();
            graph.Title.Text = "";
            graph.Title.Style.Font.Size = new FontUnit(26, UnitType.Pixel);
            graph.XLabelStyle.Color = "#9933CC";
            graph.XLabelStyle.Size = 10;
            graph.YLegend.Text = "Votes";
            graph.YLegend.Size = 12;
            graph.YLegend.Color = "0x736AFF";
            graph.XLegend.Text = "Your question";
            graph.XLegend.Size = 14;
            graph.XLegend.Color = "0x736AFF";
            graph.YTicks.Size = 2;

            DataRenderer<int> ret = new DataRenderer<int>();
            ret.GraphTypeRenderer = CreateGraphType();
            graph.XLabels.Values.Add("Answer 1");
            graph.XLabels.Values.Add("Answer 2");
            graph.XLabels.Values.Add("Answer 3");
            graph.XLabels.Values.Add("Answer 4");
            graph.XLabels.Values.Add("Answer 5");
            ret.Values.Add(3);
            ret.Values.Add(7);
            ret.Values.Add(12);
            ret.Values.Add(1);
            ret.Values.Add(3);
            graph.Values.Add(ret);
            graph.YMax = 13;
            graph.Attributes.Add(new KeyValuePair(Tokens.BackgroundColor, "#FFFFFF"));

            e.Graph = graph;
        }

        private IValueSetRenderer CreateGraphType()
        {
            string selectedVal = ddlGraphType.SelectedValue;
            string grColor = "E61C1C";
            int alpha = 60;
            if (selectedVal == GlassGraphType.GlassGraphToken)
            {
                GlassGraphType t = new GlassGraphType(alpha, '#' + grColor, "", null);
                return t;
            }
            else if (selectedVal == ThreeDimGraphType.ThreeDimGraphToken)
            {
                ThreeDimGraphType t = new ThreeDimGraphType(alpha, '#' + grColor, "", null);
                return t;
            }
            else if (selectedVal == PieGraphType.PieGraphToken)
            {
                PieGraphType t = new PieGraphType();
                t.Colors = new List<string>(new string[] {"E61C1C", "462CDE", "3F9735"});
                t.Alpha = alpha;
                return t;
            }
            else if (selectedVal.Equals(FadeBarGraphType.FadeBarGraphToken))
            {
                return new FadeBarGraphType(alpha, "#" + grColor, "", null);
            }
            else if (selectedVal.Equals(SketchGraphType.SketchGraphToken))
            {
                return new SketchGraphType(alpha, 5, "#" + grColor, "", null);
            }
            else if (selectedVal.Equals(LineGraphType.LineGraphToken))
            {
                return new LineGraphType(2, "#" + grColor, "", null);
            }
            else if (selectedVal.Equals(DotLineGraphType.DotLineGraphToken))
            {
                return new DotLineGraphType(2, 4, "#" + grColor, "DotLines", 10);
            }
            else if (selectedVal.Equals(HollowLineGraphType.HollowLineGraphToken))
            {
                return new HollowLineGraphType(2, 4, "#" + grColor, "Hollow Lines", 10);
            }
            else
            {
                // default is bar graph
                BarGraphType t = new BarGraphType(alpha, "#" + grColor, "", null);
                return t;
            }
        }
    }
}
